<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:type:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete()"
          v-hasPermi="['system:type:remove']"
        >
          删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="typeList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品类目名称" align="center" prop="serverType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:type:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:type:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改政务云服务器类型对话框 -->
    <el-dialog
      :title="form.serverTypeId ? '修改类型' : '新增类型'"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        @submit.native.prevent
      >
        <el-form-item label="产品类目名称" prop="serverType">
          <el-input v-model="form.serverType" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPublicType, getPublicType, delPublicType, addPublicType, updatePublicType } from "@/api/system/type";

export default {
  name: "Type",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      total: 0, // 总条数
      typeList: [], // 政务云服务器类型表格数据
      open: false, // 是否显示弹出层
      // 查询参数
      queryParams: {
        params:{
          pageNum: 1,
          pageSize: 10,
        },
        serverType: null,
      },
      form: {}, // 表单参数
      rules: {}, // 表单校验
      isShake:false,//防抖开关
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询政务云服务器类型列表 */
    getList() {
      this.loading = true;
      listPublicType(this.queryParams)
        .then((res) => {
          this.typeList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serverType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.serverTypeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isShake = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serverTypeId = row.serverTypeId || this.ids;
      getPublicType(serverTypeId).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.serverTypeId != null) {
            updatePublicType(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.isShake) {
              addPublicType(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.isShake = false;
            });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serverTypeIds = row ? [row.serverTypeId] : this.ids;
      this.$modal
        .confirm('是否确认删除政务云服务器类型编号为"' + serverTypeIds + '"的数据项？')
        .then(() => {
          delPublicType(serverTypeIds).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
